﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Cngot.Extensions.EventBus.RabbitMQ.Integration;

namespace Cngot.Extensions.EventBus.RabbitMQ.Producers
{
    public class RabbitClientProducer : BaseProducerPool, IRabbitClientProducer
    {
        RabbitOptions rabbitOptions;
        RabbitProducerOptions rabbitProducerOptions;
        public RabbitClientProducer(RabbitProducerOptions rabbitProducerOptions, RabbitOptions rabbitOptions) : base(rabbitOptions)
        {
            this.rabbitOptions = rabbitOptions;
            this.rabbitProducerOptions = rabbitProducerOptions;
        }

        protected override int InitializeCount => rabbitProducerOptions.InitializeCount;

        /// <summary>
        /// 普通的往队列发送消息
        /// </summary>
        /// <param name="messages"></param>
        /// <param name="options"></param>
        public async Task PublishAsync(string[] messages, MessageOptions options = null)
        {
            var producer = RentProducer();

            foreach (var queue in rabbitProducerOptions.Queues)
            {
                if (!string.IsNullOrEmpty(queue))
                {
                    await producer.PublishAsync(queue, messages, null, options);
                }
            }

            ReturnProducer(producer);
        }
        /// <summary>
        /// 使用交换机发送消息
        /// </summary>
        /// <param name="routingKey"></param>
        /// <param name="messages"></param>
        /// <param name="options"></param>
        public async Task PublishAsync(string routingKey, string[] messages, MessageOptions options = null)
        {
            var producer = RentProducer();

            await producer.PublishAsync(rabbitOptions.Exchange, routingKey, messages, options);

            ReturnProducer(producer);
        }

    }
}
